package com.kefu.call.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.call.http.client.CtiClient;
import com.kefu.call.model.CallSipuser;
import com.kefu.call.model.CdrCustomer;
import com.kefu.call.service.CallSipuserService;
import com.kefu.call.service.CdrCustomerService;
import com.kefu.call.vo.CallSipuserInsertVo;
import com.kefu.call.vo.CallSipuserSearchVo;
import com.kefu.call.vo.CallSipuserUpdateVo;
import com.kefu.call.vo.CallSipuserVo;
import com.kefu.common.annotation.Permission;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.sys.mapper.SysTenantMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author CodeGenerator
 * @date 2022/07/24
 */
@Api(tags = "SIP用户")
@RestController
@RequestMapping("call/sipuser")
public class CallSipuserController extends AbstractBaseController {

    @Autowired
    private CallSipuserService callSipuserService;
    @Autowired
    CdrCustomerService cdrCustomerService;
    @Autowired
    CtiClient ctiClient;

    @PostMapping("/add")
    @ApiOperation(value = "新增SIP用户")
    public Result<String> add(@Validated @RequestBody CallSipuserInsertVo vo) {
        logger.info("新增SIP用户:{}", vo);
        String tid = KefuContext.getTid();
        CdrCustomer cdrCustomer = cdrCustomerService.getOne(Wrappers.lambdaQuery(CdrCustomer.class).eq(CdrCustomer::getCompany, tid));
        if(cdrCustomer==null){
            throw BusinessException.msg("账号未初始化");
        }
        if(CollUtil.isNotEmpty(vo.getSipUsers()) && StrUtil.isNotBlank(vo.getPassword())){
            vo.getSipUsers().stream().limit(100).forEach(sipUser->{
                if(sipUser!=null && sipUser.length()==9 && sipUser.startsWith("2"+cdrCustomer.getOid())){
                    if (callSipuserService.count(Wrappers.lambdaQuery(CallSipuser.class).eq(CallSipuser::getSipUser, sipUser)) == 0) {
                        vo.setCompany(tid);
                        vo.setSipUser(sipUser);
                        CallSipuser info = vo.convert();
                        callSipuserService.save(info);
                    }

                }
            });
            ctiClient.reloadFsCache("directory");
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除SIP用户")
    public Result<String> delete(@Validated @RequestBody CallSipuserVo delete) {
        logger.info("删除SIP用户:{}", delete);

        callSipuserService.deleteById(delete.getSipUser());
        return ResultGenerator.genSuccessResult();
    }



    @PostMapping("/list")
    @ApiOperation(value = "查询SIP用户")
    public Result<Page<CallSipuser>> list(@Validated @RequestBody CallSipuserSearchVo search) {

        Page<CallSipuser> page = callSipuserService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }



}
